package com.example.provider.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.common.base.BaseController;
import com.example.common.base.Paging;
import com.example.common.base.Result;
import com.example.common.model.UserAttention;
import com.example.common.model.condition.UserAttentionCondition;
import com.example.provider.mapper.UserAttentionMapper;
import com.example.provider.service.Impl.UserAttentionServiceImpl;
import com.example.provider.service.UserAttentionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 用户关注的人关系表Controller
 * 
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Api(tags = "用户关注的人关系表服务")
@RestController
@RequestMapping("/userAttention")
public class UserAttentionController extends BaseController {
    @Autowired
    private UserAttentionService userAttentionService;
    @Autowired
    private UserAttentionMapper userAttentionMapper;
    @Autowired
    private UserAttentionServiceImpl userAttentionServiceImpl;

    /**
     * 关注用户
     */
    @GetMapping("/attentionUser")
    public Boolean attentionUser(String userId, String beNoticedUserId){
        return userAttentionServiceImpl.attentionUser(userId, beNoticedUserId);
    }

    /**
     * 取消关注用户
     */
    @GetMapping("reAttentionUser")
    public Boolean reAttentionUser(String userId, String beNoticedUserId){
        return userAttentionServiceImpl.reAttentionUser(userId, beNoticedUserId);
    }

    @RequestMapping("/text")
    public Map<String,List> textUserAttention(){
        System.out.println(("----- selectAll method test ------"));
        List<UserAttention> userList = userAttentionMapper.selectList(null);
        Map map=new HashMap();
        map.put("test",userList);
        return map;
//        System.out.println(userList);
//        userList.forEach(System.out::println);
    }

    @ApiOperation(value = "根据查询参数分页查询用户关注的人关系表列表")
    @ApiImplicitParam(name = "condition", value = "用户关注的人关系表查询条件", required = true, dataType = "UserAttentionCondition", paramType = "body")
    @PostMapping("/list")
    public Paging<UserAttention> list(@RequestBody UserAttentionCondition condition) {
        IPage<UserAttention> page = userAttentionService.findUserAttentionByCondition(condition);
        return Paging.buildPaging(page);
    }

    @ApiOperation(value = "根据主键ID查询用户关注的人关系表信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @GetMapping(value = "/get/{id}")
    public Result<UserAttention> get(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("请选择需要查询的数据！");
        }
        UserAttention userAttention = userAttentionService.getUserAttentionById(id);
        return Result.ok(userAttention);
    }

    @ApiOperation(value = "新增用户关注的人关系表信息")
    @ApiImplicitParam(name = "userAttention", value = "用户关注的人关系表", required = true, dataType = "UserAttention", paramType = "body")
    @PostMapping("/add")
    public Result<?> add(@RequestBody UserAttention userAttention) {
        boolean bool = userAttentionService.addUserAttention(userAttention);
        if (bool) {
            return Result.ok(userAttention);
        }
        return Result.failed();
    }

    @ApiOperation(value = "修改用户关注的人关系表信息")
    @ApiImplicitParam(name = "userAttention", value = "用户关注的人关系表", required = true, dataType = "UserAttention", paramType = "body")
    @PutMapping(value = "/update")
    public Result<?> update(@RequestBody UserAttention userAttention) {
        Integer id = userAttention.getId();
        if (id == null) {
            return Result.failed("请选择需要修改的数据！");
        }
        boolean bool = userAttentionService.updateUserAttention(userAttention);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID删除用户关注的人关系表")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = userAttentionService.deleteUserAttentionById(id);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID列表批量删除用户关注的人关系表")
    @ApiImplicitParam(name = "idList", value = "主键ID列表", required = true, allowMultiple = true, paramType = "body")
    @DeleteMapping("/deleteList")
    public Result<?> deleteList(@RequestBody List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = userAttentionService.deleteUserAttentionByIds(idList);
        return Result.okOrFailed(bool);
    }
}